package com.xuda.springboot.config;

import com.xuda.springboot.controller.UserController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ：程序员徐大大
 * @description：TODO
 * @date ：2022-03-21 20:17
 */
@Configuration
public class LoginHandlerInterceptor implements HandlerInterceptor {
    private static final Logger log = LoggerFactory.getLogger(LoginHandlerInterceptor.class);
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       log.info("session+=》{}",request.getSession().getAttribute("user"));
        //登录成功后，应该有用户得session
        Object loginuser = request.getSession().getAttribute("user");
        if (loginuser == null) {
            request.setAttribute("loginmsg", "没有权限请先登录");
            request.getRequestDispatcher("/login.html").forward(request, response);
            return false;
        } else {
        return true;
        }
    }

}
